package com.example.coursemgt.controller;

import com.example.coursemgt.model.Course;
import com.example.coursemgt.service.CourseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class CourseController {
    private final static Logger log = LoggerFactory.getLogger(CourseController.class);
    @Resource
    private CourseService courseService;

    @GetMapping("/ping")
    public String ping() {
        log.info("ping");
        return "pong";
    }

    @GetMapping("/top")
    public List<Course> top() {
        log.info("top");
        return courseService.top();
    }

    @GetMapping("/queryAll")
    public List<Course> queryAll() {
        log.info("queryAll");
        List<Course> lists = courseService.queryAll();
        return lists;
    }

    @PostMapping("/course")
    public Course course() {
        log.info("create a course");
        try {
            return courseService.create();
        } catch (Exception e) {
            log.error("create course failed: {}", e.getMessage());
            return null;
        }
    }

    @PostMapping("/scheduleCourse")
    public Course scheduleCourse(@RequestParam long schoolId, @RequestParam long teacherId) {
        log.info("schedule a course");
        try {
            return courseService.scheduleCourse(schoolId, teacherId);
        } catch (Exception e) {
            log.error("schedule a course failed: {}", e.getMessage());
            return null;
        }
    }

    @GetMapping("/cachedCourseById")
    public Course cachedCourseById(@RequestParam Long id) {
        try {
            log.info("find cached course by id");
            Course course = courseService.cachedCourseById(id);
            return course;
        } catch (Exception e) {
            log.error("find cached course failed: {},{}", id, e.getMessage());
            return null;
        }
    }

    @GetMapping("/findCourseById")
    public Course findCourseById(@RequestParam Long id) {
        try {
            log.info("find course by id");
            Course course = courseService.findCourseById(id);
            return course;
        } catch (Exception e) {
            log.error("find course failed: {},{}", id, e.getMessage());
            return null;
        }
    }

    @PostMapping("/updateCourse")
    public String updateCourse(@RequestBody Course course) {
        log.info("updateCourse");
        int result = courseService.updateCourse(course);
        if (result != 0) {
            return "update course success";
        }
        log.error("update course failed");
        return "failed";
    }

    @DeleteMapping("/deleteCourseById")
    public String deleteCourseById(@RequestParam Long id) {
        log.info("delete course by id");
        int result = courseService.deleteCourseById(id);
        if (result != 0) {
            return "delete success";
        }
        log.error("delete course failed");
        return "delete failed";
    }
}
